﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Cellula.Model;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Web.Support;
using Cn.Edwon.Spring;
using Cn.Edwon.Web;
using Cn.Cellula.Biz;
using Cn.Cellula.Model.Product;
using Cn.Cellula.Model.System;

namespace Cn.Cellula.Website.Admin.Content.Atl {
    public partial class EditOpt : PageSupport {
        IItemBiz biz = BizFactory.Get<IItemBiz>();
        String OptKey = null;
        String AltKey = null;
        String CategoryKey = null;
        protected void Page_Load(object sender, EventArgs e) {
            AltKey = Request.QueryString["t"];
            OptKey = Request.QueryString["id"];
            CategoryKey = Request.QueryString["cid"];
            if (!Page.IsPostBack) {
                InitForm();
                if (!String.IsNullOrEmpty(OptKey)) {
                    FillForm(OptKey);
                }
            }
        }

        private void InitForm() {
            try {
                VSysAtlEO eo = biz.Load<VSysAtlEO>(AltKey);
                this.lab_AltName.Text = eo.AtlName;
            } catch { }
            try {
                String path = BizFactory.Get<ICategoryBiz>().LoadCategoryPath(this.OwnerKey, null, CategoryKey, " / ");
                this.lab_CategoryName.Text = (String.IsNullOrEmpty(path)) ? "所有分类" : path;
            } catch { }

        }

        /// <summary>
        /// 表单初始化数据
        /// </summary>
        /// <param name="typeKey"></param>
        private void FillForm(String typeKey) {
            TSysAtlOptionsEO eo = biz.Load<TSysAtlOptionsEO>(typeKey);
            this.txt_OptName.Text = eo.OptText;
            this.txt_OrderNumber.Text = eo.OrderNumber.ToString();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_save_Click(object sender, EventArgs e) {
            //---- 增加新项 ---------------------------------
            if (String.IsNullOrEmpty(OptKey)) {
                TSysAtlOptionsEO eo = new TSysAtlOptionsEO();
                eo.AtlKey = AltKey;
                eo.OptText = this.txt_OptName.Text;
                eo.OptValue = this.txt_OptName.Text;
                try {
                    eo.OrderNumber = Convert.ToInt32(this.txt_OrderNumber.Text);
                } catch { }
                biz.Save<TSysAtlOptionsEO>(eo);
                BizFactory.Get<ILogBiz>().LogInsert("属性选择项", eo.OptText, this.Actor, Request);
            } else {//更新----------------------------------
                TSysAtlOptionsEO eo = biz.Load<TSysAtlOptionsEO>(OptKey);
                eo.OptText = this.txt_OptName.Text;
                eo.OptValue = this.txt_OptName.Text;
                try {
                    eo.OrderNumber = Convert.ToInt32(this.txt_OrderNumber.Text);
                } catch { }
                biz.Update<TSysAtlOptionsEO>(eo);
                BizFactory.Get<ILogBiz>().LogUpdate("属性选择项", eo.OptText, this.Actor, Request);
            }
            Response.Redirect("List.aspx?cid=" + CategoryKey + "&t=" + AltKey);
        }
    }
}
